<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of shippingcom
 *
 * @author hassanali
 */
class banks extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/banks_model');
    }

    public function index() {
        $falid_id = $this->banks_model->is_found();

        $data['count'] = count($falid_id);
        $data['result'] = $this->banks_model->filter();
        $data['pagination'] = $this->admin_pagination('index', count($this->banks_model->filter(true)), 10);
        $this->themes->render('banks/index', $data);
    }

    public function view($id = FALSE) {
        
        if($id){
            $falid_id = $this->banks_model->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $data['result'] = $falid_id;
            $this->themes->render('banks/view', $data);
        }else{
            show_404();
        }
    }

    public function edit($id = false) {
        if ($id) {
            $id_id = $this->banks_model->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $pass['bank'] = $id_id;
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());

                $this->form_validation->set_rules('title', lang('label_name'), 'trim|required');
                $this->form_validation->set_rules('owner', lang('label_owner'), 'trim|required');
                $this->form_validation->set_rules('number', lang('label_number'), 'trim|required');
                $this->form_validation->set_rules('iban', lang('label_ibn'), 'trim|required');

                if ($this->form_validation->run()) {
                    if ($this->input->post('publish')) {
                        $pub = 'YES';
                    } else {
                        $pub = 'NO';
                    }

                    $data['user_update'] = $this->admin_id;
                    $data['title'] = $this->input->post('title');
                    $data['owner'] = $this->input->post('owner');
                    $data['number'] = $this->input->post('number');
                    $data['iban'] = $this->input->post('iban');
                    $data['publish'] = $pub;
                    if ($this->banks_model->save($data, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            } else {
                $this->themes->render('banks/edit', $pass);
            }
        } else {
            show_404();
        }
    }

    public function add() {

        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());

            $this->form_validation->set_rules('title', lang('label_name'), 'trim|required');
            $this->form_validation->set_rules('owner', lang('label_owner'), 'trim|required');
            $this->form_validation->set_rules('number', lang('label_number'), 'trim|required');
            $this->form_validation->set_rules('iban', lang('label_ibn'), 'trim|required');

            if ($this->form_validation->run()) {
                if ($this->input->post('publish')) {
                    $pub = 'YES';
                } else {
                    $pub = 'NO';
                }

                $data['user_id'] = $data['user_update'] = $this->admin_id;
                $data['title'] = $this->input->post('title');
                $data['owner'] = $this->input->post('owner');
                $data['number'] = $this->input->post('number');
                $data['iban'] = $this->input->post('iban');
                $data['publish'] = $pub;
                if ($this->banks_model->save($data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_add_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render('banks/add');
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'delete': {
                    $this->banks_model->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->banks_model->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $this->banks_model->delete(array('id' => $id), FALSE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }
    
    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->banks_model->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->banks_model->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->banks_model->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->banks_model->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->banks_model->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->banks_model->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }
    
    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->banks_model->filter(TRUE, FALSE, TRUE);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->banks_model->filter(FALSE, $offset, TRUE);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('banks/trash', $this->pass);
    }

}

?>
